package com.yanqu.road.server.cmd.activity.dafuweng;

import com.yanqu.road.entity.activity.dafuweng.DFWUserData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDFWDamage;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.pb.DaFuWengPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_DA_FU_WENG_CROSS_TEN_ROLL_RESULT, desc = "")
public class DaFuWengCrossTenRollResultCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyProto.DfwTenRollCrossRespMsg respMsg = MonopolyProto.DfwTenRollCrossRespMsg.parseFrom(packet.getBytes());
        String needAddReward = respMsg.getNeedAddReward();
        Property needReward = new Property();
        if (!StringUtils.isNullOrEmpty(needAddReward)) {
            player.getModule(CurrencyModule.class).addCurrency(needAddReward, eLogMoneyType.DaFuWeng, eLogMoneyType.DaFuWengBattleBoss);
            Property property = PropertyHelper.parseStringToProperty(needAddReward);
            needReward.addProperty(property);
        }
        int activityId = respMsg.getActivityId();
        DFWUserData userData = DaFuWengMgr.getUserData(activityId, player.getUserId());

        // 加积分
        BigInteger scoreAddValue = BigInteger.ZERO;
        for (Map.Entry<Integer, BigInteger> entry : needReward.getGoods().entrySet()) {
            if (eItemId.DFW_GOLDEN.getValue() == entry.getKey()) {
                scoreAddValue = entry.getValue();
                userData.setGoldScore(userData.getGoldScore() + entry.getValue().intValue());
                player.notifyListener(eGamePlayerEventType.DaFuWengScoreRank.getValue(), userData.getGoldScore());
                player.notifyListener(eGamePlayerEventType.DaFuWengUnionScoreRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(userData.getGoldScore()), player.getUserInfo().getUnionUid()));
            }
        }
        int realDamage = respMsg.getRealDamage();
        // 排行榜
        userData.setTotalDamage(userData.getTotalDamage() + realDamage);
        player.notifyListener(eGamePlayerEventType.DaFuWengDamageRank.getValue(), userData.getTotalDamage());
        player.notifyListener(eGamePlayerEventType.DaFuWengUnionDamageRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(userData.getTotalDamage()), player.getUserInfo().getUnionUid()));

        String rewardData = respMsg.getRewardData();
        int addPower = respMsg.getAddPower();
        List<MonopolyProto.MonopolyUnionMsg> unionMsgListList = respMsg.getUnionMsgListList();
        if (respMsg.getZaoyao()) {
            if (unionMsgListList.size() == 2) {
                userData.setZaoYaoOptionalList(DaFuWengPb.parseZaoYaoOptionalList(unionMsgListList));
            }else {
                userData.setEventId(DaFuWengMgr.getEmptyEventId(DaFuWengMgr.getDfwConfig()));
                userData.setComplete(true);
            }
        }

        MonopolyProto.TenRollDiceRespMsg.Builder resp = MonopolyProto.TenRollDiceRespMsg.newBuilder();
        resp.setRet(ret);
        resp.setReward(rewardData);
        resp.setEnergyRecovery(addPower);
        resp.setUserData(DaFuWengPb.getUserDataMsg(userData));
        resp.setRollTimes(respMsg.getRollTimes());
        player.sendPacket(ClientProtocol.U_DA_FU_WENG_TEN_ROLL, resp);

        for (MonopolyProto.DfwAttackBossLogMsg logMsg : respMsg.getDfwBossLogList()) {
            AutoLogMgr.add(new LogDFWDamage(activityId, userData.getUserId(), player.getUserInfo().getUnionUid(), logMsg.getDamage(), logMsg.getKill(), logMsg.getBossId(),
                    logMsg.getBossType(), logMsg.getReward(), System.currentTimeMillis()));
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
